//页面加载
var redirectUrl;
$(document).ready(function () {
    redirectUrl = getQueryString("referer");
    $('#username').val("");
    $('#password').val("");
    if ($.cookie("rmbUser") == "true") {
        $("#remember-password").removeClass("radiuIconN").addClass("radiuIconY").attr("data-status","1");
        $("#username").val($.cookie("userName"));
        $("#password").val($.cookie("passWord"));
    }
});
//登录js
function plogin() {
    var username = $("#username").val();
    var password = $("#password").val();
    if(username=="" || password==""){
        $("#loginMsg").html("用户名或者密码不能为空!");
        $("#loginMsg").css("color","red");
        setTimeout(function(){
            $("#loginMsg").html("");
        },2000);
        return false;
    };
    $.ajax({
        //几个参数需要注意一下
        type: "POST",//方法类型
        async: false, // 使用同步方式
        dataType: "json",//预期服务器返回的数据类型
        url: "/plogin" ,//url
        data: {"imei":username,"password":password,"mark":"login"},
        success: function (result) {
            if (result.status == 1) {
                rmbUser();
                if(redirectUrl == null || redirectUrl==""||redirectUrl == undefined || redirectUrl =="/wechaTest.html") {
                    window.location.href = "/RealTimeLocation.html?v="+Math.random();
                }else{
                    window.location.href =redirectUrl+"?v="+Math.random();
                }
            }else{
                $("#loginMsg").html(result.msg);
                $("#loginMsg").css("color","red");
                setTimeout(function(){
                    $("#loginMsg").html("");
                },2000);
            }
        },
        error : function() {
            $("#loginMsg").html("网络异常!");
            $("#loginMsg").css("color","red");
            setTimeout(function(){
                $("#loginMsg").html("");
            },2000);
        }
    });
};
//记住登录信息
function rmbUser(){
    //存储变量
    var D_status = $("#remember-password").data("status");
    username = $('#username').val();
    password = $('#password').val();
    if(D_status == 1){
        //未选中状态
        $.cookie("rmbUser", "true", { expires: 7 }); // 存储一个带7天期限的 cookie 是否已经选择记录用户名
        $.cookie("userName", username, { expires: 7 }); // 存储一个带7天期限的 cookie
        $.cookie("passWord", password, { expires: 7 }); // 存储一个带7天期限的 cookie
    }else{
        $.cookie("rmbUser", null);        // 删除 cookie
        $.cookie("userName", null);
        $.cookie("passWord", null);
    }
}

function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}